<?php
require_once 'phpmoko/lib/common.php';

require_once 'phpmoko/ui/HasAlignment.php';

/**
 * Base class for all PHPMoko drawable elements
 * We supose that every widget has its dimension
 * and it's aligned to any position.
 *
 * @author borsanza
 */
class Widget implements HasAlignment
{
	private static $uniqueId = 0;
	private $horizontal = null;
	private $vertical = null;
	protected $id = null;
	private $width = null;
	private $height = null;
	protected $style = null;
	private $visible = true;

	protected function __construct()
	{
		$this->vertical = HasVerticalAlignment::ALIGN_TOP;
		$this->id = Widget::getUniqueId();
	}

	static function getUniqueId()
	{
		self::$uniqueId++;
		return 'single_' . self::$uniqueId;
	}

	/**
	 * (non-PHPdoc)
	 *
	 * @see HasHorizontalAlignment::getHorizontalAlignment()
	 */
	public function getHorizontalAlignment()
	{
		return $this->horizontal;
	}

	/**
	 * (non-PHPdoc)
	 *
	 * @see HasHorizontalAlignment::setHorizontalAlignment()
	 */
	public function setHorizontalAlignment($align)
	{
		$this->horizontal = $align;
	}

	/**
	 * (non-PHPdoc)
	 *
	 * @see HasVerticalAlignment::getVerticalAlignment()
	 */
	public function getVerticalAlignment()
	{
		return $this->vertical;
	}

	/**
	 * (non-PHPdoc)
	 *
	 * @see HasVerticalAlignment::setVerticalAlignment()
	 */
	public function setVerticalAlignment($align)
	{
		$this->vertical = $align;
	}

	public function setStyleName($style)
	{
		$this->style = $style;
	}

	public function addStyleName($style)
	{
		if ($this->style == null) {
			$this->setStyleName($style);
			return;
		}

		$this->style .= ' ' . $style;
	}

	/**
	 * Set unique id for the widget
	 *
	 * @param integer $id
	 */
	public function setId($id)
	{
		$this->id = $id;
	}

	public function getId()
	{
		return $this->id;
	}

	public function toString()
	{
		return '';
	}

	public function setWidth($width)
	{
		$this->width = $width;
	}

	public function getWidth()
	{
		return $this->width;
	}

	public function setHeight($height)
	{
		$this->height = $height;
	}

	public function getHeight()
	{
		return $this->height;
	}

	public function setSize($width, $height)
	{
		$this->setWidth($width);
		$this->setHeight($height);
	}

	public function setVisible($visible)
	{
		$this->visible = $visible;
	}

	public function isVisible()
	{
		return $this->visible;
	}

	static function toStringSafe($object)
	{
		if ($object instanceof Widget)
			return $object->toString();

		if ($object == null)
			$object = '';

		$widget = new HTML($object);
		return $widget->toString();
	}
}
?>